package com.magist.manage.web.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.magist.core.entity.Page;
import com.magist.core.exception.BusinessException;
import com.magist.core.service.SimpleServiceImpl;
import com.magist.manage.copyrights.entity.Communication;
import com.magist.manage.copyrights.entity.ContractRights;
import com.magist.manage.copyrights.entity.Copyrights;
import com.magist.manage.copyrights.entity.SupplyContract;
import com.magist.manage.copyrights.entity.SupplyInfo;
import com.magist.manage.copyrights.service.CommunicationService;
import com.magist.manage.copyrights.service.ContractRightsService;
import com.magist.manage.copyrights.service.CopyrightsService;
import com.magist.manage.copyrights.service.SupplyContractService;
import com.magist.manage.copyrights.service.SupplyInfoService;
import com.magist.manage.web.entity.UserInfo;
import com.magist.manage.web.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Service
public class UserInfoServiceImpl extends SimpleServiceImpl<UserInfo> implements UserInfoService {

    @Autowired
    private SupplyInfoService supplyInfoService;
    @Autowired
    private SupplyContractService supplyContractService;
    @Autowired
    private CopyrightsService copyrightsService;
    @Autowired
    private ContractRightsService contractRightsService;
    @Autowired
    private CommunicationService communicationService;

    @Override
    public PageInfo<UserInfo> page(Example example, Page page) {
        PageHelper.startPage(page.getCurrPage(), page.getLimit());
        List<UserInfo> list = this.find(example);
        PageInfo<UserInfo> pageInfo = new PageInfo(list);

        return pageInfo;
    }

    @Override
    public List<UserInfo> findByUserName(String userName) {
        UserInfo userInfo = new UserInfo();
        userInfo.setUserName(userName);
        return this.find(userInfo);
    }

    @Override
    public void doDataAuthUser(String userId, String authByUserId) {
        // supplyInfo,supplyContract,copyrights,contractRights,communication
        UserInfo userInfo = this.getById(userId);
        UserInfo authUser = this.getById(authByUserId);
        if(ObjectUtils.isEmpty(userInfo)) {
            throw new BusinessException("当前用户为空");
        }
        if(ObjectUtils.isEmpty(authUser)) {
            throw new BusinessException("对接用户为空");
        }
        userInfo.setAuthById(authByUserId);
        userInfo.setAuthByName(authUser.getUserName());
        this.update(userInfo);

        List<SupplyInfo> supplyInfos = supplyInfoService.findByCreateById(userId);
        if (!CollectionUtils.isEmpty(supplyInfos)) {
            supplyInfos.forEach(supplyInfo -> {
                supplyInfo.setAuthById(authByUserId);
                supplyInfoService.update(supplyInfo);
            });

        }
        List<SupplyContract> supplyContracts = supplyContractService.findByCreateById(userId);
        if (!CollectionUtils.isEmpty(supplyContracts)) {
            supplyContracts.forEach(supplyContract -> {
                supplyContract.setAuthById(authByUserId);
                supplyContractService.update(supplyContract);
            });

        }
        List<Copyrights> copyrightses = copyrightsService.findByCreateById(userId);
        if (!CollectionUtils.isEmpty(copyrightses)) {
            copyrightses.forEach(copyrights -> {
                copyrights.setAuthById(authByUserId);
                copyrightsService.update(copyrights);
            });

        }
        List<ContractRights> contractRightses = contractRightsService.findByCreateById(userId);
        if (!CollectionUtils.isEmpty(contractRightses)) {
            contractRightses.forEach(contractRights -> {
                contractRights.setAuthById(authByUserId);
                contractRightsService.update(contractRights);
            });

        }
        List<Communication> communications = communicationService.findByCreateById(userId);
        if (!CollectionUtils.isEmpty(communications)) {
            communications.forEach(communication -> {
                communication.setAuthById(authByUserId);
                communicationService.update(communication);
            });

        }
    }
}